package src.controller.mainInterface;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class habitdata implements Serializable {
    private String category;
    private String goalName;
    private boolean completed;
    private int progress;

    public habitdata(String category, String goalName) {
        this.category = category;
        this.goalName = goalName;
        this.completed = false;
        this.progress = 0;
    }

    // Getters and Setters
    public String getCategory() { return category; }
    public String getGoalName() { return goalName; }
    public boolean isCompleted() { return completed; }
    public int getProgress() { return progress; }

    public void setCompleted(boolean completed) { this.completed = completed; }
    public void setProgress(int progress) { this.progress = progress; }

    // 保存数据到文件
    public static void saveHabits(List<habitdata> habits, String filename) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
            oos.writeObject(habits);
            System.out.println("习惯数据已保存到: " + filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 从文件加载数据
    @SuppressWarnings("unchecked")
    public static List<habitdata> loadHabits(String filename) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
            return (List<habitdata>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载习惯数据，将使用默认数据");
            return createDefaultHabits();
        }
    }

    // 创建默认习惯数据
    private static List<habitdata> createDefaultHabits() {
        List<habitdata> habits = new ArrayList<>();
        String[] categories = {"学习", "日常", "运动"};

        for (String category : categories) {
            for (int i = 1; i <= 3; i++) {
                habits.add(new habitdata(category, "目标" + i));
            }
        }

        return habits;
    }
}